package com.ztstkj.lzscale.ui

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.proembed.service.MyService
import com.sscl.baselibrary.utils.DebugUtil
import com.ztstkj.lzscale.R
import com.ztstkj.lzscale.adpters.CategoryItem
import com.ztstkj.lzservices.networks.NetworkUtils
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit

open class BaseActivity : AppCompatActivity() {
    protected val tag: String = this.javaClass.simpleName
    protected var mXService: MyService? = null
    private var schedulerNet: ScheduledExecutorService? = null
    protected val categorys = listOf(
        CategoryItem("易腐垃圾", R.drawable.category_perishable.toString(), 1, 0),
        CategoryItem("其它垃圾", R.drawable.category_other.toString(), 3, 0),
        CategoryItem("可回收物", R.drawable.category_recyclable.toString(), 2, 0),
        CategoryItem("有害垃圾", R.drawable.category_harmful.toString(), 4, 0)
    )
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        checkNet()
    }

    private fun checkNet() {
        if (schedulerNet?.isShutdown == false) return
        schedulerNet = Executors.newScheduledThreadPool(1)
        schedulerNet?.scheduleWithFixedDelay({
            NetworkUtils.onLine = NetworkUtils.isConnected(this)
            DebugUtil.debugOut("网络检查:" + NetworkUtils.onLine)
        }, 1, 60, TimeUnit.SECONDS)
    }
    protected fun updateUserFragment(fragment: Fragment) {
        //在activity中可以直接调用getSupportFragmentManager方法获取FragmentManager
        val fragmentManager = supportFragmentManager
        //开启事务
        val transaction = fragmentManager.beginTransaction()
        //向容器内添加或替换fragment，一般用replace方法
        transaction.replace(R.id.frameLayoutUserCard, fragment)
        //提交事务
        transaction.commit()
    }
    protected fun updateNavFragment(fragment: Fragment) {
        //在activity中可以直接调用getSupportFragmentManager方法获取FragmentManager
        val fragmentManager = supportFragmentManager
        //开启事务
        val transaction = fragmentManager.beginTransaction()
        //向容器内添加或替换fragment，一般用replace方法
        transaction.replace(R.id.frameMainNav, fragment)
        //提交事务
        transaction.commit()
    }
    override fun onDestroy() {
        super.onDestroy()

        schedulerNet?.shutdown()
    }
}